package com.ChenYaRou.www.controller;

import com.ChenYaRou.www.po.DetailedUser;
import com.ChenYaRou.www.po.User;
import com.ChenYaRou.www.utils.GenerateSql;
import com.ChenYaRou.www.utils.JdbcUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import static com.ChenYaRou.www.constant.False.EMPTY;
import static com.ChenYaRou.www.constant.JsonBeanConstant.SUCCESS;

/**
 * @ClassName HomePageServletController
 * @Description TODO
 * @Author 86183
 * @Date 2022/5/12 23:18
 * @Version 1.0
 **/
public class HomePageServletController {
    public void  updateDetailUser(HttpServletRequest request, HttpServletResponse response){
        try {
            String name = request.getParameter("name");
            String id = request.getParameter("ownerId");
            String telephone = request.getParameter("telephone");
            String money = request.getParameter("money");
            String district = request.getParameter("district");
            String street = request.getParameter("street");
            String detailAddress = request.getParameter("detailAddress");

            if(EMPTY.equals(money)) {
                response.sendRedirect("Something.jsp?state=money could not be empty");
                return;
            }
            if(Float.parseFloat(String.valueOf(money))<0){
                response.sendRedirect("Something.jsp?state=money could not be a negative");
                return;}
            DetailedUser detailedUser=new DetailedUser(
                    Integer.parseInt(id),telephone,district,street,detailAddress,Float.parseFloat(money));
            new ShopServletController().makePicture(request,detailedUser);
            DetailedUser detailedUserOld=new DetailedUser();
            detailedUserOld.setUserId(Integer.valueOf(id));

            String updateDetailUser= GenerateSql.getInstance().update(detailedUser,detailedUserOld);

            if(name!=null){
                User userOld=new User();
                userOld.setId(Integer.valueOf(id));

                User userNew=new User();
                userNew.setName(name);

                String updateUser=GenerateSql.getInstance().update(userNew,userOld);
                int success = JdbcUtil.getInstance().insertOrUpdateMore(updateDetailUser, updateUser);
                if(success>0){
                    response.sendRedirect("Something.jsp?state="+SUCCESS);
                }else{
                    response.sendRedirect("Something.jsp?state=fail update");
                }
            }
        } catch (IllegalAccessException | IOException | ServletException | InvocationTargetException | NoSuchMethodException e) {
            e.printStackTrace();
        }


    }
}
